﻿using cma.cimiss;
using cma.cimiss.client;
using SXMeteorological.Data.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SXMeteorological.Data.Utility
{
    class Program
    {
        static void Main(string[] args)
        {
            double xMin=105.489060;//x最小值
            double xMax=111.246800; //x最大者
            double yMin=31.712748; //y最小值
            double yMax = 39.582336;
            string interfaceName = "getSurfEleInRegionByTime";
            string blnpath = string.Empty;
            Dictionary<string, string> params1 = new Dictionary<string, string>();
            params1.Add("dataCode", "SURF_CHN_MUL_HOR"); // 资料代码
            params1.Add("elements", "Lat,Lon,TEM");// 检索要素：站号、小时降水
            params1.Add("times", "20151207000000"); // 检索时间
            params1.Add("adminCodes", "610000");
            // 可选参数
            params1.Add("staLevels", "011,012,013");
            string mapName = "陕西省逐小时降雨量图";
            //Render render = new Render(xMin, xMax, yMin, yMax, params1, interfaceName, "气温", blnpath,mapName);
            //render.RenderMap();
        }
        public static void GetDatFileData(string interfaceId)
        {
          // interfaceId = "getSurfEleByTime";
            interfaceId = "getSurfEleInRegionByTime";
            string datFile = string.Empty;

            DataQueryClient client = new DataQueryClient();
            Dictionary<String, String> params1 = new Dictionary<String, String>();
            // 必选参数

            params1.Add("dataCode", "SURF_CHN_MUL_HOR"); // 资料代码
            params1.Add("elements", "Lat,Lon,TEM");// 检索要素：站号、小时降水
            params1.Add("times", "20151207000000"); // 检索时间20151209060000
            params1.Add("adminCodes", "610000");
            // 可选参数
            params1.Add("staLevels", "011,012,013");
          //  params1.Add("orderby", "Station_ID_C:ASC"); // 排序：按照站号从小到大
            RetArray2D retArr2d=new RetArray2D();
            /* 3.   调用接口 */
            try
            {
                // 初始化接口服务连接资源
                client.initResources();

                int result = client.callAPI_to_array2D(CIMISSPara._uerID, CIMISSPara._passWord, interfaceId, params1, retArr2d);
               string[][] strs= retArr2d.data;
               var path = AppDomain.CurrentDomain.BaseDirectory;
               //临时目录
               var tempDir = Path.Combine(path, "temp");
               if (!Directory.Exists(tempDir))
               {
                   Directory.CreateDirectory(tempDir);
               }
               var guid = Guid.NewGuid().ToString();
               var currDir = Path.Combine(tempDir, guid);
               Directory.CreateDirectory(currDir);

               //散列点文件
               using (var dataStreamW = File.CreateText(Path.Combine(currDir, "data.dat")))
               {
                   for (var i = 0; i < strs.Length; i++)
                   {
                       string[] s = strs[i];

                       dataStreamW.WriteLine(string.Format("{0}\t{1}\t{2}",s[0], s[1],s[2]));
                   }
                   dataStreamW.Flush();
                   dataStreamW.Close();
               };
            }
       catch (Exception e)
            {

            }
            finally
            {
                
          
                // 释放接口服务连接资源

                client.destroyResources();

            }
        }
    }
}
